package com.unibuy.smartdevice.devices;

import android.util.Log;

import androidx.core.view.InputDeviceCompat;

import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.exception.LogsParseException;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

public class
DevNFCPay {
    private Logs logs;
    private final byte ACK = 6;
    private final byte ETX = 3;
    private final byte NAK = 21;
    private final byte STX = 2;

    private String type = "NCCC";
    private String swipeString = "";
    private String dataLength = "";
    private boolean isScaning = false;

    public DevNFCPay() {
        this.logs = new Logs(this.getClass());
    }

    //    public DevNFCPay() {
//        String[] SerialConfig = "/dev/ttymxc2,9600".split(",");
//        String pathName = SerialConfig[0];
//        int baudrate = Integer.parseInt(SerialConfig[1]);
//        if (SerialConfig.length > 2) {
//            dataLength = SerialConfig[2];
//            type = "TSIB";
//        } else {
//            type = "NCCC";
//        }
//    }

    public byte[] checkout() { //NCCC信用卡結帳
        String date = new SimpleDateFormat("yyMMddHHmmss", Locale.US).format(new Date());
        String str = "I       50                                            " + date + "                                                                                                                                                                                                                                                                                                                                              ";
        byte[] data = new byte[403];
        data[0] = STX;
        System.arraycopy(str.getBytes(), 0, data, 1, str.length());
        data[401] = ETX;
        for (int i = 1; i < 402; i++) {
            data[402] = (byte) (data[402] ^ data[i]);
        }
        return data;
    }



    public byte[] reset() { //重啟
        byte[] data = new byte[403];
        data[0] = STX;
        System.arraycopy("I       99".getBytes(), 0, data, 1, "I       99".length());
        data[401] = ETX;
        for (int i = 1; i < 402; i++) {
            data[402] = (byte) (data[402] ^ data[i]);
        }
        return data;
    }

    /*
    170000483***86   00000202206JS2  00900248                                                                                                                                                   cI0702 01300084*69  000201J2000115                                               i
    \u0006
    192130096******11  000000248 0000794    1846                                              200014  M                   A\/MdC+4PLjtONdrF=                                   M
    \u0006
    1N 56222231        030047
    \u0006
    \u0006
    9006*7  0000242325064   0015700                               020014        3x9iC+4uPL6jtO+eNbUAredrVfpnFL4=       T01
    \u0003
    */

    private HashMap<String, String> decode(String s) {
        s.substring(1, 2);
        s.substring(2, 8);
        s.substring(9, 11);
        s.substring(11, 12);
        s.substring(12, 14);
        s.substring(15, 20);
        s.substring(20, 39);
        s.substring(39, 43);
        s.substring(43, 55);
        s.substring(55, 61);
        s.substring(61, 67);
        s.substring(67, 76);
        s.substring(76, 77);
        String C15_ResponseCode = s.substring(77, 81);
        s.substring(81, 96);
        s.substring(96, 104);
        s.substring(116, 134);
        s.substring(134, 135);
        s.substring(175, 177);
        s.substring(177, 189);
        s.substring(189, 201);
        s.substring(201, 213);
        s.substring(213, 215);
        s.substring(215, 221);
        s.substring(223, 224);
        s.substring(224, 232);
        s.substring(232, 240);
        s.substring(240, 252);
        s.substring(InputDeviceCompat.SOURCE_KEYBOARD, 307);
        s.substring(307, 313);
        s.substring(313, 314);
        s.substring(314, 315);
        s.substring(315, 318);

        String responseCode = C15_ResponseCode;
        String status = String.valueOf(s.charAt(1));
        String category = swipeString.length()>0? swipeString.substring(1): "";
        String message = s.substring(1);

        logs.info("responseCode:" + responseCode);
        logs.info("status:" + status);
        logs.info("category:" + category);
        logs.info("message:" + message);

        HashMap<String, String> responseMap = new HashMap<>();
        responseMap.put("code", responseCode);
        responseMap.put("status", status);
        responseMap.put("category", category);
        responseMap.put("message", message);

        return responseMap;
    }

    public HashMap<String, String> read(String rxData) throws LogsParseException {
        int stx = rxData.lastIndexOf(String.format("%c", Byte.valueOf(STX)));
        int etx = rxData.indexOf(String.format("%c", Byte.valueOf(ETX)));
        while (stx > etx) {
            stx = rxData.lastIndexOf(String.format("%c", Byte.valueOf(STX)), stx - 1);
        }
        if (stx >= 0) {
            return decode(rxData.substring(dataLength.length() + stx, etx));
        } else {
            throw new LogsParseException(logs, "Decode parsing failed");
        }
    }









}
